package com.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;

import java.io.*;
import java.util.List;

public class OssClient {


    public static void main(String[] args) {

    }

    public static void upload(){
        String endpoint = "";
        String accessKeyId = "";
        String accessKeySecret = "";
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        String bucketName = "";
        String filePath = "";
        byte[] b = new byte[]{};
        InputStream inputstream = new ByteArrayInputStream(b);
        ossClient.putObject(bucketName, filePath, inputstream);
        ossClient.putObject(bucketName, filePath, new File(""));
    }

//    private String getFilePath(String sourceFileName) {
//        DateTime dateTime = new DateTime();
//        return "images/" + dateTime.toString("yyyy")
//                + "/" + dateTime.toString("MM") + "/"
//                + dateTime.toString("dd") + "/" + System.currentTimeMillis() +
//                RandomUtils.nextInt(100, 9999) + "." +
//                StringUtils.substringAfterLast(sourceFileName, ".");
//    }

    public static void list() {
        String endpoint = "";
        String accessKeyId = "";
        String accessKeySecret = "";
        String bucketName = "";
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 设置最大个数。
        final int maxKeys = 200;
        // 列举文件。
        ObjectListing objectListing = ossClient.listObjects(new ListObjectsRequest(bucketName).withMaxKeys(maxKeys));
        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
        System.out.println(sums);
    }

    public static void delete(String objectName) {
        String endpoint = "";
        String accessKeyId = "";
        String accessKeySecret = "";
        String bucketName = "";
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 根据BucketName,objectName删除文件
        ossClient.deleteObject(bucketName, objectName);
    }
    public void exportOssFile(OutputStream os, String objectName) throws IOException {
        String endpoint = "";
        String accessKeyId = "";
        String accessKeySecret = "";
        String bucketName = "";
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
        OSSObject ossObject = ossClient.getObject(bucketName, objectName);
        // 读取文件内容。
        BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
        BufferedOutputStream out = new BufferedOutputStream(os);
        byte[] buffer = new byte[1024];
        int lenght = 0;
        while ((lenght = in.read(buffer)) != -1) {
            out.write(buffer, 0, lenght);
        }
        if (out != null) {
            out.flush();
            out.close();
        }
        if (in != null) {
            in.close();
        }
    }
}
